import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@core/services/http.client';

@Component({
  selector: 'app-student',
templateUrl: './student.component.html'
})
export class StudentComponent implements OnInit {
    popTitle;
    selectValue1;
    selectValue2;
    loading = false;
    modalVisible3 = false;
    Sname = '';
    SClass = '';
    Sschool = '';
    Sphone = '';
    Semail = '';
    Sstate = '';
    _allChecked = false;
    _indeterminate = false;
    _displayData = [];
    _checkTable = [];
    _checkLength = 0;
    ids = [];
    option1 = [
        { value: '', label: '请选择' },
        { value: '0', label: '班级1' },
        { value: '1', label: '班级2' },
        { value: '2', label: '班级3' }
    ];
    option2 = [
        { value: '', label: '请选择' },
        { value: '0', label: '在读' },
        { value: '1', label: '结业' },
        { value: '2', label: '作废' }
    ];
    saveState = '';
    searchValue = '';
    serchName = {};
    _loading = false;
    formNews = {};
    data = [];
    Status = [];
    sta = '';

    ngOnInit() {
        this.getData();
    }

    getData() {
        // this._loading = true;
        const type = {pi: 1, ps: 10};
        this.http.post('EduManagement/student/list', type).subscribe(res => {
            // this._loading = false;
            // this.data = res;
        });
        this. data = [
            {
                S_id: '1',
                S_name: '范菲菲',
                S_college: 'aaa',
                S_class_name: '班级1',
                S_phone: '13633221000',
                S_email: '123@qq.com',
                S_status: '2',
                S_class_id: '0'
            },
            {
                S_id: '2',
                S_name: '范菲菲',
                S_college: 'aaa',
                S_class_name: '班级3',
                S_phone: '13633221000',
                S_email: '123@qq.com',
                S_status: '1',
                S_class_id: '2'
            },
            {
                S_id: '3',
                S_name: '范菲菲',
                S_college: 'aaa',
                S_class_name: '班级2',
                S_phone: '13633221000',
                S_email: '123@qq.com',
                S_status: '0',
                S_class_id: '1'
            },
            {
                S_id: '4',
                S_name: '范菲菲',
                S_college: 'aaa',
                S_class_name: '班级3',
                S_phone: '13633221000',
                S_email: '123@qq.com',
                S_status: '1',
                S_class_id: '2'
            }
        ];
        this.data.map(d => {
            if (d.S_status == '0') {
                this.sta = '在读';
            } else if (d.S_status == '1') {
                this.sta = '结业';
            }else if (d.S_status == '2') {
                this.sta = '作废';
            }
            this.Status.push(this.sta);
        });
    }
    save() {
        this.loading = true;
        setTimeout(() => {
            this.modalVisible3 = false;
            this.loading = false;
            this.createMessage('success', '操作成功！');
        }, 500);
        if (this.saveState === 'add') {
            this.formNews = {S_name: this.Sname, S_college: this.Sschool, S_class_id: this.selectValue1, S_phone: this.Sphone, S_email: this.Semail, S_status: this.selectValue2 }
        }else {
            this.formNews = {S_id: '', S_name: this.Sname, S_college: this.Sschool, S_class_id: this.selectValue1, S_phone: this.Sphone, S_email: this.Semail, S_status: this.selectValue2  }
        }
        this.http.post('EduManagement/student/' + this.saveState, this.formNews).subscribe(res => {
            this.loading = false;
            this.getData();
            // this.createMessage('success', '操作成功！');
        });
    }

    modal (state, data) {
        if (state === 'add') {
            this.modalVisible3 = true;
            this.Sname = '';
            this.SClass = '';
            this.Sschool = '';
            this.Sphone = '';
            this.Semail = '';
            this.Sstate = '';
            this.selectValue1 = '';
            this.selectValue2 = '';
            this.popTitle = '新建学生';
            this.saveState = 'add';
        }else if (state === 'update') {
            this.popTitle = '修改学生';
            this.modalVisible3 = true;
            this.Sname = data.S_name;
            this.SClass = data.S_class_name;
            this.Sschool = data.S_college;
            this.Sphone = data.S_phone;
            this.Semail = data.S_email;
            this.selectValue1 = data.S_class_id;
            this.selectValue2 = data.S_status;
            this.saveState = 'update';
        }

    }

    search () {
        this.serchName = {name: this.searchValue}
        this.http.post('', this.serchName).subscribe(res => {
        });
    }

    showConfirm = (data) => {
        this.confirmServ.confirm({
            title  : '您是否确认要删除这项内容',
            onOk() {
                const id = {id: data.id};
                // this.http.post('EduManagement/student/del', id).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    _displayDataChange($event) {
        this._displayData = $event;
        this._refreshStatus();
    }

    _refreshStatus() {
        const allChecked = this._displayData.every(value => value.checked === true);
        const allUnChecked = this._displayData.every(value => !value.checked);
        const checkTable = this._displayData.filter(value => value.checked);
        this._allChecked = allChecked;
        this._indeterminate = (!allChecked) && (!allUnChecked);
        this._checkTable = checkTable;
        this._checkLength = checkTable.length;
    }

    _checkAll(value) {
        if (value) {
            this._displayData.forEach(data => {
                data.checked = true;
            });
        } else {
            this._displayData.forEach(data => {
                data.checked = false;
            });
        }
        this._refreshStatus();
    }

    allDelete () {
        this._checkTable.map(m => {
            this.ids.push(m.id);
        });
        this.confirmServ.confirm({
            title  : '您是否确认要批量删除选中的' + this._checkLength + '项内容？',
            onOk: () => {
                // this.http.post('EduManagement/student/del', {id: this.ids}).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    info(contentTpl, data, status) {
        this.Sname = data.S_name;
        this.SClass = data.S_class_name;
        this.Sschool = data.S_college;
        this.Sphone = data.S_phone;
        this.Semail = data.S_email;
        this.selectValue1 = data.S_class_id;
        this.selectValue2 = status;
        this.confirmServ.info({
            title: '查看详情',
            content: contentTpl
        });
    }

    createMessage = (type, text) => {
        this._message.create(type, `${text}`);
    }

    constructor(
        private confirmServ: NzModalService,
        private http: _HttpClient,
        private _message: NzMessageService
    ) {}

}
